package dog.weatherwallpaper.model;

import org.json.JSONObject;

import dog.weatherwallpaper.R;
import dog.weatherwallpaper.utils.App;
import dog.weatherwallpaper.utils.Strings;

public final class Location {
	private String areaName = App.context().getString(R.string.area);
	private String country = App.context().getString(R.string.country);
	private String region = App.context().getString(R.string.region);
	@SuppressWarnings("unused")
	private String latitude = "";
	@SuppressWarnings("unused")
	private String longitude = "";
	@SuppressWarnings("unused")
	private int population;
	@SuppressWarnings("unused")
	private String weatherUrl;

	public Location(final String json) {
		if (Strings.isNullOrEmpty(json)) { return; }
		try {
			JSONObject data = new JSONObject(json).optJSONObject("search_api").optJSONArray("result").optJSONObject(0);
			this.areaName = Strings.getValueFromArray(data, "areaName");
			this.country = Strings.getValueFromArray(data, "country");
			this.region = Strings.getValueFromArray(data, "region");
			this.latitude = data.optString("latitude");
			this.longitude = data.optString("longitude");
			this.population = data.optInt("population");
			this.weatherUrl = Strings.getValueFromArray(data, "weatherUrl");
		} catch (Exception e) {
			App.sendMail(e);
		}
	}

	/**
	 * Returns "areaName, region, country\n".
	 */
	public String get() {
		return areaName + ", " + region + ", " + country + '\n';
	}
}